#include "Quest.h"
#include "GFuns.h"
#include "Game.h"

class QuestCaravan : public Quest
{
	public:
		enum ECaravan
		{
			QUEST_STATUS_HORSE = 3,
			QUEST_STATUS_AFTER_TALK
		};

		QuestCaravan(Character* aSmith);
		virtual ~QuestCaravan(){};

		virtual Phrase* GetPhrase(Character* aCharacter);
		virtual void OnAction(EMenuAction aAction,
			ActionData aArg1 = ActionData(), ActionData aArg2 = ActionData(), ActionData aArg3 = ActionData()){};

	private:
		Item spareWhell;
		Item sword;
		Character* smith;
};

QuestCaravan::QuestCaravan(Character* aSmith) :
	spareWhell(ITEM_QUEST, QUEST_SPARE_WHEEL),
	sword(ITEM_WEAPON, WEAPON_SHORT_SWORD),
	smith(aSmith)
{
	smith->quests.push_back(this);
}

Phrase* QuestCaravan::GetPhrase(Character* aCharacter)
{
	string sentence;
	Character* hero = Game::GetHero();
	phrase = nullptr;

	if(aCharacter == smith && progress != QUEST_STATUS_TAKEN)
		return nullptr;

	if(progress == QUEST_STATUS_NEW)
	{
		Phrase* Aa = Phrase::CreatePhrase("Welcome to our city.");
		Phrase* A = Phrase::CreatePhrase("How can I help you?");

		Phrase* B = Phrase::CreatePhrase("Carry on then.");
		
		Phrase* Da = Phrase::CreatePhrase("Actually there is one thing");
		Phrase* Db = Phrase::CreatePhrase("you could do for this city.");
		Phrase* Dc = Phrase::CreatePhrase("The caravan will soon depart.");
		Phrase* Dd = Phrase::CreatePhrase("But they lost a wheel.");
		Phrase* De = Phrase::CreatePhrase("Find it and bring it to me.");
		
		sentence = "You can ask " + smith->GetName() + ", our smith, for one.";
		
		Phrase* Dg = Phrase::CreatePhrase(sentence);
		Phrase* D = Phrase::CreatePhrase("Will you save our caravan?");

		Phrase* E = Phrase::CreatePhrase("Good. Start immediately.");

		Phrase* Fa = Phrase::CreatePhrase("The correct answer is \"yes\", obviously.");
		Phrase* F = Phrase::CreatePhrase("(sigh) Tourists.");
		
		Phrase* Ga = Phrase::CreatePhrase("That can be arranged.");
		Phrase* Gb = Phrase::CreatePhrase("Ask the city cook to make you");
		Phrase* Gc = Phrase::CreatePhrase("horse hoof stew, or I will");
		Phrase* G = Phrase::CreatePhrase("cut your tongue off!");

		phrase = Aa;
		Aa->nextPhrase = A;
		A->AddOption("I'm fine, thank you.",B);
		A->AddOption("Do you have any tasks for me?",Da);

		Da->nextPhrase = Db;
		Db->nextPhrase = Dc;
		Dc->nextPhrase = Dd;
		Dd->nextPhrase = De;
		De->nextPhrase = Dg;
		Dg->nextPhrase = D;

		D->AddOption("I will help you if you'll pay me.",E);
		D->AddOption("I don't want any trouble. What is the correct answer?",Fa);
		D->AddOption("I would rather eat a horse's hoof.",Ga);
		D->AddOption("Yes.",E);

		Fa->nextPhrase = F;
		F->nextPhrase = D;

		Ga->nextPhrase = Gb;
		Gb->nextPhrase = Gc;
		Gc->nextPhrase = G;
		G->SetNextQuestStatus(QUEST_STATUS_HORSE,this);

		E->SetNextQuestStatus(QUEST_STATUS_TAKEN,this);
	}
	else if(progress == QUEST_STATUS_HORSE)
	{
		Phrase* Aa = Phrase::CreatePhrase("Have you reconsidered my offer?");

		Phrase* B = Phrase::CreatePhrase("Good. Start immediately.");

		Phrase* C = Phrase::CreatePhrase("Get away then!");

		phrase = Aa;
		Aa->AddOption("I will help you if you'll pay me.",B);
		Aa->AddOption("I'll help you.",B);
		Aa->AddOption("Find someone else.",C);

		B->SetNextQuestStatus(QUEST_STATUS_TAKEN,this);

		C->SetNextQuestStatus(QUEST_STATUS_NEW,this);
	}
	else if(progress == QUEST_STATUS_TAKEN && aCharacter != smith && !hero->HaveItem(&spareWhell))
	{
		Phrase* Ha = Phrase::CreatePhrase("What are you doing?");
		Phrase* Hb = Phrase::CreatePhrase("Don't come back without the wheel!");
		Phrase* H = Phrase::CreatePhrase("Go talk to " + smith->GetName() + ".");

		phrase = Ha;
		Ha->nextPhrase = Hb;
		Hb->nextPhrase = H;
	}
	else if(progress == QUEST_STATUS_TAKEN && aCharacter == smith)
	{
		Phrase* Ha = Phrase::CreatePhrase("Howdy!");
		Phrase* Hb = Phrase::CreatePhrase("Are you here for a spare wheel?");
		Phrase* Hc = Phrase::CreatePhrase("Here, take it.");
		Phrase* H = Phrase::CreatePhrase("Now get back to " + owner->GetName() + ".");

		hero->GetInventory()->AddItem(spareWhell);

		phrase = Ha;
		Ha->nextPhrase = Hb;
		Hb->nextPhrase = Hc;
		Hc->nextPhrase = H;

		H->SetNextQuestStatus(QUEST_STATUS_AFTER_TALK, this);
	}
	else if(hero->HaveItem(&spareWhell))
	{
		Phrase* Ea = Phrase::CreatePhrase("You did well so I will");
		Phrase* Eb = Phrase::CreatePhrase("not cut your hand off...");
		Phrase* Ec = Phrase::CreatePhrase("Just kidding!");
		Phrase* Ed = Phrase::CreatePhrase("Here is your reward.");
		Phrase* E = Phrase::CreatePhrase("And behave. I'm watching you.");

		phrase = Ea;
		Ea->nextPhrase = Eb;
		Eb->nextPhrase = Ec;
		Ec->nextPhrase = Ed;
		Ed->nextPhrase = E;

		E->SetNextQuestStatus(QUEST_STATUS_DONE, this);
		hero->GetInventory()->AddItem(sword);
		hero->GetInventory()->RemoveItem(&spareWhell);
	}

	return phrase;
}

Quest* GetQuestCaravan(Character* aSmith)
{
	return new QuestCaravan(aSmith);
}